package com.snail.snailvr.views.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.snail.snailvr.R;
import com.snail.snailvr.model.VideoInfo;
import com.snail.snailvr.network.providers.downloads.DownloadHelper;
import com.snail.snailvr.network.providers.downloads.DownloadManager;
import com.snail.snailvr.utils.DialogUtils;
import com.snail.snailvr.utils.IntentUtils;
import com.snail.snailvr.widget.DonutProgress;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by snail on 16/7/1.
 */

public abstract class BaseDownloadViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    abstract void onSuccess(VideoInfo videoInfo);

    abstract void onPause(VideoInfo videoInfo);

    abstract void onRunning(VideoInfo videoInfo);

    abstract void onWait(VideoInfo videoInfo);

    abstract void onFail(VideoInfo videoInfo);

    abstract void onInit(VideoInfo videoInfo);

    abstract void switchDownloadState(VideoInfo videoInfo,int viewId);//手动更改按钮状态


    @Bind(R.id.download_status_icon)
    ImageView mDownloadStatus;
    @Nullable
    @Bind(R.id.play_online)
    ImageView mPlayOnline;

    @Nullable
    @Bind(R.id.donut_download_progress)
    DonutProgress mProgressBar;
    private Context mContext;

    public BaseDownloadViewHolder(Context context,View itemView) {
        super(itemView);
        mContext=context;
        ButterKnife.bind(this, itemView);
    }

    protected void updateDownloadState(int status, VideoInfo video) {
        setDownloadTag(video);
        switch (status) {
            case DownloadManager.STATUS_RUNNING:
                onRunning(video);
                break;
            case DownloadManager.STATUS_PENDING:
                onWait(video);
                break;
            case DownloadManager.STATUS_PAUSED:
                if (video.getReason() == DownloadManager.PAUSED_WAITING_TO_RETRY)
                    onFail(video);
                else {
                    onPause(video);
                }
                break;
            case DownloadManager.STATUS_SUCCESSFUL:
                onSuccess(video);
                break;
            case DownloadManager.STATUS_FAILED:
                onFail(video);
                break;
            case DownloadManager.STATUS_INIT:
                onInit(video);
                break;

        }

    }

    private void setDownloadTag(VideoInfo video) {
        setTag(mDownloadStatus,video);
        if(mProgressBar!=null){
            setTag(mProgressBar,video);
        }
        if (mPlayOnline != null) {
            setTag(mPlayOnline,video);
        }
    }

    private void setTag(View view,VideoInfo video) {
        view.setTag(video);
        view.setClickable(true);
        view.setOnClickListener(this);
    }

    @Override
    public void onClick(final View v) {
       final VideoInfo videoInfo=(VideoInfo) v.getTag();

        if(videoInfo!=null) {
            final int status=videoInfo.getDownloadState();
            //switchDownloadState(videoInfo,v.getId());
            //v.setClickable(false);
            if(v.getId()==R.id.download_status_icon) {

                if(status != DownloadManager.STATUS_RUNNING && DialogUtils.showJudgeNetDialog(null, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        changeDownlaodState(videoInfo, status);
                    }
                }))
                    return;
                changeDownlaodState(videoInfo, status);
            }else if(v.getId()==R.id.donut_download_progress){

                if (status == DownloadManager.STATUS_PAUSED) {
                    if(DialogUtils.showJudgeNetDialog(null, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            DownloadHelper.getDownloadManager().resumeDownload(videoInfo.getVideoId());
                            ((DonutProgress)v).setDownloadStatusAndRefresh(DownloadManager.STATUS_RUNNING);
                        }
                    }))
                        return;
                    DownloadHelper.getDownloadManager().resumeDownload(videoInfo.getVideoId());
                    ((DonutProgress)v).setDownloadStatusAndRefresh(DownloadManager.STATUS_RUNNING);
                } else if (status == DownloadManager.STATUS_RUNNING) {
                    DownloadHelper.getDownloadManager().pauseDownload(videoInfo.getVideoId());
                    ((DonutProgress)v).setDownloadStatusAndRefresh(DownloadManager.STATUS_PAUSED);
                }
            } else if (v.getId() == R.id.play_online) {
                if(videoInfo.getDownloadState()==DownloadManager.STATUS_SUCCESSFUL) {
                    String videoPath = DownloadHelper.getVideoPath(videoInfo.getVideoId());
                    if(videoPath!=null)
                        IntentUtils.playVideo(mContext, videoPath, videoInfo.getSName());
                }else {
                    if(DialogUtils.showJudgeNetDialog(null,new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            IntentUtils.playVideo(mContext, videoInfo.getCVideoUrl(), videoInfo.getSName());
                        }
                    }))
                        return;
                    IntentUtils.playVideo(mContext, videoInfo.getCVideoUrl(), videoInfo.getSName());
                }
            }
        }
    }

    private void changeDownlaodState(VideoInfo videoInfo, int status) {
        switch (status) {
            case DownloadManager.STATUS_PAUSED:
                DownloadHelper.getDownloadManager().resumeDownload(videoInfo.getVideoId());
                break;
            case DownloadManager.STATUS_RUNNING:
                DownloadHelper.getDownloadManager().pauseDownload(videoInfo.getVideoId());
                break;
            case DownloadManager.STATUS_FAILED:
                DownloadHelper.restartDownload(mContext, videoInfo.getVideoId());
                break;
            case DownloadManager.STATUS_SUCCESSFUL:
//                        IntentUtils.playVideo(mContext, videoInfo.getVideoPath(), videoInfo.getSName());
                break;
            case DownloadManager.STATUS_INIT:
                if (!TextUtils.isEmpty(videoInfo.getCDownloadUrl()) && videoInfo.getIBannerId() > 0) {
                    DownloadHelper.startDownloadDiscoverVideo(videoInfo.getCDownloadUrl(), videoInfo.getIBannerId(), videoInfo.getSName());
                }
                break;
        }
    }


}
